package com.ossjk.oa.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.util.Constant;
import com.ossjk.core.util.CryptoUtils;
import com.ossjk.core.vo.ZtreeBean;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.entity.Post;
import com.ossjk.oa.system.service.IBankcardService;
import com.ossjk.oa.system.service.IContactsService;
import com.ossjk.oa.system.service.IEmployeePostService;
import com.ossjk.oa.system.service.IEmployeeService;
import com.ossjk.oa.system.service.IPostService;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author chair
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/system/employee")
public class EmployeeController extends BaseController {
	@Autowired
	private IEmployeeService iEmployeeService;
	@Autowired
	private IContactsService iContactsService;
	@Autowired
	private IBankcardService iBankcardService;
	@Autowired
	private IPostService iPostService;
	@Autowired
	private IEmployeePostService iEmployeePostService;

	@RequiresPermissions("00000000")
	@GetMapping("/list")
	public Object list(String did, String rname, String email, String mobile, Page page) {
		// 列表内容
		Wrapper wrapper = Condition.create().isWhere(true).orderBy("e.crtm", true);
		if (!CommonUtil.isBlank(did)) {
			wrapper.eq("d.id", did);
		}
		if (!CommonUtil.isBlank(rname)) {
			wrapper.like("e.rname", rname);
		}
		if (!CommonUtil.isBlank(email)) {
			wrapper.like("e.email", email);
		}
		if (!CommonUtil.isBlank(mobile)) {
			wrapper.like("e.mobile", mobile);
		}
		page = iEmployeeService.selectVoPage(page, wrapper);
		return responseSuccess(page);
	}

	@RequiresPermissions("000000000015")
	@PostMapping("/insert")
	public Object insert(Employee employee) {
		Wrapper wrapper = Condition.create().eq("name", employee.getName()).eq("isdelete", "1");
		if (!CommonUtil.isBlank(iEmployeeService.selectList(wrapper))) {
			return responseFail("登陆名字已存在。。");
		}
		Wrapper wrapper2 = Condition.create().eq("email", employee.getEmail()).eq("isdelete", "1");
		if (!CommonUtil.isBlank(iEmployeeService.selectList(wrapper2))) {
			return responseFail("邮件已存在。");
		}

		// 默认密码
		employee.setPwd(CryptoUtils.encodeMD5(Constant.SYSTEM_DEFAUL_PWD));
		if (iEmployeeService.insert(employee)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000000000000")
	@PostMapping("/updateDetail")
	public Object updateDetail(Employee employee, String[] cname, String[] crelationship, String[] cphone, String[] bcard, String[] bbank, String[] bmain, String[] areaIds) {
		// 省
		employee.setPrid(areaIds[0]);
		// 市
		employee.setCtid(areaIds[1]);
		// 区
		employee.setArid(areaIds[2]);
		if (iEmployeeService.updateDetail(employee, cname, crelationship, cphone, bcard, bbank, bmain)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000000000000")
	@GetMapping("/toUpdateDetail/{id}")
	public Object toUpdateDetail(@PathVariable("id") String id) {
		Map map = new HashMap();
		Employee employee = iEmployeeService.selectById(id);
		map.put("employee", employee);
		Wrapper wrapper = Condition.create().like("eid", id);
		map.put("contacts", iContactsService.selectList(wrapper));
		Wrapper wrapper2 = Condition.create().like("eid", id);
		map.put("bankcard", iBankcardService.selectList(wrapper));
		// 回显省市区
		List areaIds = new ArrayList();
		areaIds.add(employee.getPrid());
		areaIds.add(employee.getCtid());
		areaIds.add(employee.getArid());
		map.put("areaIds", areaIds);
		return responseSuccess(map);
	}

	@RequiresPermissions("00000000")
	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		return responseSuccess(iEmployeeService.selectById(id));
	}

	@RequiresPermissions("00000000")
	@PutMapping("/update")
	public Object update(Employee employee) {
		if (iEmployeeService.updateById(employee)) {
			return responseSuccess();
		} else {
			return responseFail();
		}

	}

	@RequiresPermissions("000000000010")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iEmployeeService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("000000000025")
	@GetMapping("/toUpdatePost")
	public Object toUpdatePost() {
		// 查找所有部门
		Wrapper wrapper = Condition.create().orderBy("level", true).orderBy("sort", true);
		List<Post> posts = iPostService.selectList(wrapper);
		List<ZtreeBean> ztreeBeans = new ArrayList();
		// 转换ztreeBean
		if (!CommonUtil.isBlank(posts)) {
			for (Post post : posts) {
				ZtreeBean ztreeBean = new ZtreeBean();
				ztreeBean.setId(post.getId());
				ztreeBean.setPId(post.getPid());
				ztreeBean.setName(post.getName());
				ztreeBean.setOpen(true);
				ztreeBeans.add(ztreeBean);
			}
		}
		return responseSuccess(ztreeBeans);
	}

	@RequiresPermissions("000000000025")
	@PutMapping("/updatePost")
	public Object updatePost(String id, String[] pid) {
		if (iEmployeePostService.updatePost(id, pid)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
